package com.resou.reader.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.resou.reader.R;
import com.resou.reader.utils.NetworkUtils;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.view.Loading;
import com.resou.reader.view.NetErrorFragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.jessyan.autosize.internal.CustomAdapt;

public abstract class ResouBaseFragment<P extends ResouBasePresenter> extends Fragment implements CustomAdapt {
    protected Activity mActivity;
    protected P mPresenter;
    protected Unbinder unbinder;
    private NetErrorFragment mNetErrorFragment;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
    }

    private static final String TAG = "BaseFragment-App";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
//        bindView();
        unbinder = ButterKnife.bind(this, view);
        initView();
        initPresenter();
        if (mPresenter != null) {
            mPresenter.subscribe();
        }
        return view;
    }

//    protected abstract void bindView();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setRetainInstance(true);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.unsubscribe();
        }
        if (unbinder != null) {
            unbinder.unbind();
        }
        dissLoading();
    }

    protected abstract int getLayoutId();

    protected abstract void initPresenter();

    protected abstract void initView();

    protected abstract void retryRefreshData();

    /**
     * 显示网络异常视图
     *
     * @param resId
     */
    public void showNetErrorView(int resId) {
        if (mNetErrorFragment == null) {
            mNetErrorFragment = new NetErrorFragment(new NetErrorFragment.OnClick() {
                @Override
                public void onClick() {
                    if (NetworkUtils.isNetworkConnected(getContext())) {
                        retryRefreshData();
                        dismissNetErrorView();
                    } else {
                        ToastUtil.makeShortToast("网络异常");
                    }
                }
            });
        }
        mNetErrorFragment.show(resId, getChildFragmentManager());
    }

    /**
     * 关闭无网络页码
     */
    public void dismissNetErrorView() {
        if (mNetErrorFragment != null) {
            mNetErrorFragment.dismiss();
        }
    }

    /**
     * 显示进度条
     */
    public void showLoading() {
        if (!Loading.getInstance().isShow()) {
            Loading.getInstance().show(getFragmentManager());
        }
    }

    /**
     * 自定义显示加载圈布局
     */
    public void setLoadingView(int view, String message) {
        Loading.getInstance().setView(view);
        Loading.getInstance().setLoadingText(message);
    }

    /***
     * 取消加载
     */
    public void dissLoading() {
        if (Loading.getInstance().isShow()) {
            Loading.getInstance().dismiss();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        dismissNetErrorView();
    }

    @Override
    public boolean isBaseOnWidth() {
        return true;
    }

    @Override
    public float getSizeInDp() {
        return 375;
    }
}
